package adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ViewGroup;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.callback.ImageFixCallback;

import java.util.List;

import bean.AutoTestPaperBean;
import jp.wasabeef.richeditor.RichEditor;
import student.andlei.com.student.R;
import utils.Logger;
import utils.SPUtils;

public class AutoTestPaperAdapter extends BaseQuickAdapter<AutoTestPaperBean.mlist,BaseViewHolder>{
    private AnswerListAdapter questionListAdapter;
    private Context context;
    public AutoTestPaperAdapter(Context context, int layoutResId, @Nullable List<AutoTestPaperBean.mlist> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, final AutoTestPaperBean.mlist item) {

//        questionListAdapter = new AnswerListAdapter(R.layout.item_xuanze,item.question_options);
//        RecyclerView recyclerView = helper.getView(R.id.rcly_);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(context) {
//            @Override
//            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
//                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
//                        ViewGroup.LayoutParams.WRAP_CONTENT);
//            }
//        };
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        recyclerView.setLayoutManager(layoutManager);
//        recyclerView.setAdapter(questionListAdapter);
        RichEditor et_new_content = (RichEditor)  helper.getView(R.id.tv_content);
        Logger.i("Logger-->",""+SPUtils.getInstance(context).replaceBlank(item.question_content));
        et_new_content.setInputEnabled(false);
        et_new_content.setInitialScale(300);
        et_new_content.setHtml(SPUtils.getInstance(context).replaceBlank(item.question_content));
//        RichText.initCacheDir(context);//方法设置缓存目录
//        textView.setMovementMethod(LinkMovementMethod.getInstance());
//        RichText.fromHtml(SPUtils.getInstance(context).replaceBlank(item.question_content)).into(textView);
//        RichText.from(SPUtils.getInstance(context).replaceBlank(item.question_content)).showBorder(false).fix(imageFixCallback)
        //+"----->"+item.question_id
//                .size(ImageHolder.WRAP_CONTENT, ImageHolder.WRAP_CONTENT) .resetSize(true).scaleType(ImageHolder.ScaleType.fit_center).into(textView);
       helper.setText(R.id.tv_type_name,"题型："+item.type_name);
        // 1容易, 2较易, 3一般, 4较难, 5困难 多个用‘,’隔开，如：1,3,11
        switch (item.difficulty){
            case 1:
                helper.setText(R.id.tv_diffily_name,"难度："+"容易");
                break;
            case 2:
                helper.setText(R.id.tv_diffily_name,"难度："+"较易");
                break;
            case 3:
                helper.setText(R.id.tv_diffily_name,"难度："+"一般");
                break;
            case 4:
                helper.setText(R.id.tv_diffily_name,"难度："+"较难");
                break;
            case 5:
                helper.setText(R.id.tv_diffily_name,"难度："+"困难");
                break;
        }
        helper.setText(R.id.tv_number,"组卷次数："+item.use_num);
        helper.addOnClickListener(R.id.tv_up).addOnClickListener(R.id.tv_down).addOnClickListener(R.id.tv_delete);
        EditText editText = helper.getView(R.id.ed_input_);
        if (editText.getTag() != null && editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        editText.setText(item.score);
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.score =(s.toString());
            }
        };
        editText.addTextChangedListener(textWatcher);
        editText.setTag(textWatcher);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return super.onCreateViewHolder(parent, viewType);
    }
    private ImageFixCallback imageFixCallback = new ImageFixCallback() {
        @Override
        public void onInit(ImageHolder holder) {

        }

        @Override
        public void onLoading(ImageHolder holder) {

        }

        @Override
        public void onSizeReady(ImageHolder holder, int imageWidth, int imageHeight, ImageHolder.SizeHolder sizeHolder) {
                holder.setWidth(imageWidth/4);
                holder.setHeight((imageHeight/4)*(imageHeight/imageWidth));
        }

        @Override
        public void onImageReady(ImageHolder holder, int width, int height) {
            holder.setWidth(width/4);
            holder.setHeight((height/4)*(height/width));
        }

        @Override
        public void onFailure(ImageHolder holder, Exception e) {

        }
    };
}
